package com.jintian.smart.base.expression.fn;

import com.jintian.smart.base.api.system.provider.IEmployeeProvider;
import com.jintian.smart.kernel.common.context.ContextHolder;
import com.jintian.smart.kernel.core.annotation.beetl.BeetlFunction;
import org.apache.dubbo.config.annotation.DubboReference;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.beetl.sql.clazz.kit.BeetlSQLException;

@BeetlFunction(code = "userInfo")
public class UserInfoFunction implements Function {
    @DubboReference(check = false)
    private IEmployeeProvider employeeProvider;

    @Override
    public Object call(Object[] paras, Context ctx) {
        String prop = (String) paras[0];
        if (ContextHolder.getUserId() == null) {
            Object defaultValue = paras.length > 1 ? paras[1] : null;
            return defaultValue;
        }

        if ("name".equals(prop)) {
            return ContextHolder.getName();
        } else if ("id".equals(prop)) {
            return ContextHolder.getUserId();
        }
        // todo 地区尚未支持
//        else if ("areaCode".equals(prop)) {
//            return employeeProvider.getLoginEmpInfo(ContextHolder.getUserId()).getAreaCode();
//        }
        else {
            throw new BeetlSQLException(BeetlSQLException.SQL_SCRIPT_ERROR, "userInfo函数不支持" + prop);
        }
    }
}
